package com.erp.gather.controller;
import com.erp.gather.domain.bo.ErpCollectProductTask20Bo;
import com.erp.gather.domain.vo.ErpCollectProductTask20Vo;
import com.erp.gather.service.IErpCollectProductTask20Service;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.system.api.RemoteSysConfigService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 采集商品任务2.0
 *
 * @author erp
 * @date 2023-08-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/collectProductTask20")
public class ErpCollectProductTask20Controller extends BaseController {

    private final IErpCollectProductTask20Service iErpCollectProductTask20Service;
    @Resource
    RemoteUserService remoteUserService;

    /**
     * 查询采集商品任务2.0列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpCollectProductTask20Vo> list(ErpCollectProductTask20Bo bo, PageQuery pageQuery) {
    	TableDataInfo<ErpCollectProductTask20Vo> result = iErpCollectProductTask20Service.queryPageList(bo, pageQuery);
        result.getRows().forEach(row -> {
            try {
                //TODO 获取用户信息 内部类调用 但太慢了
                R<Object> objectSysUser = remoteUserService.selectUserById(row.getCollectUserId());
                SysUser sysUser = (SysUser)objectSysUser.getData();
                row.setCollectUserName(sysUser == null ? "" : sysUser.getNickName());
                R<Object> objectUser = remoteUserService.selectUserById(Long.valueOf(row.getCreateBy()));
                SysUser user = (SysUser)objectUser.getData();
                row.setCreateBy(user == null ? "" : user.getNickName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return result;
    }

    /**
     * 获取采集商品任务2.0详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpCollectProductTask20Vo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpCollectProductTask20Service.queryById(id));
    }

    /**
     * 新增采集商品任务2.0
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Object> add(@Validated(AddGroup.class) @RequestBody ErpCollectProductTask20Bo bo) {
    	 Map<String, Object> map = iErpCollectProductTask20Service.insertByBo(bo);
         return R.ok(map);
    }

    /**
     * 修改采集商品任务2.0
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpCollectProductTask20Bo bo) {
        return toAjax(iErpCollectProductTask20Service.updateByBo(bo));
    }

    /**
     * 删除采集商品任务2.0
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpCollectProductTask20Service.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
